package com.ruoyi.common.file;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;

@Slf4j
public class ReadFileContent {

    public static String linuxFilePath = "./file/listen_word_";

    public static List<String> readFile(String fileNum){
        String filePath = linuxFilePath + fileNum + ".txt";
        File file = new File(filePath);
        log.info("read file path: {}", filePath);
        // 读取文件内容
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            return readFileAsList(reader);
        } catch (IOException e) {
            log.error("读取失败：{}", filePath);
            throw new RuntimeException(e);
        }
    }

    private static List<String> readFileAsList(BufferedReader reader){
        try {
            Stream<String> lineStream = reader.lines(); // 流需关闭，放入 try-with-resources

            List<String> list = lineStream.filter(line -> !line.isBlank())
                    .map(key -> key.substring(10).replace(",",""))
                    .filter(key -> !key.isBlank())
                    .toList();
            log.info("select result size is : {}", list.size());
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
